<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="名称" name="sitName">
						<a-input v-model:value="searchFormState.sitName" placeholder="请输入名称" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-tree-select
						v-model:value="searchFormState.orgId"
						style="width: 100%"
						:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
						placeholder="请选择部门"
						allow-clear
						tree-default-expand-all
						:tree-data="treeData"
						:tree-default-expanded-keys="treeDefaultExpandedKeys"
						:field-names="{
							children: 'children',
							label: 'name',
							value: 'id'
						}"
					/>
				</a-col>
				<a-col :span="4">
					<a-form-item name="userStatus" :label="$t('user.userStatus')">
						<a-select v-model:value="searchFormState.userStatus" :placeholder="$t('user.placeholderUserStatus')">
							<a-select-option v-for="item in statusData" :key="item.value" :value="item.value">{{
								item.label
							}}</a-select-option>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item name="userStatus" label="在职状态">
						<a-select v-model:value="searchFormState.jobStatus" placeholder="在职状态">
							<a-select-option v-for="item in jobStatusData" :key="item.value" :value="item.value">{{
								item.label
							}}</a-select-option>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="table.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'name'">
					<div class="userInfo">
						<div class="userPic">
							<img style="width: 45px; margin-right: 10px; border-radius: 50%" :src="record.avatar" />
						</div>
						<div class="userMsg">
							<p style="font-size: 17px; color: #1f68d5; cursor: pointer">
								{{ record.name }}
							</p>
							<p>{{ record.orgName + '/' + record.positions[0].name }}</p>
						</div>
					</div>
				</template>
			</template>
		</s-table>
	</a-card>
</template>

<script setup name="roster">
	import sysSitydutyRuleApi from '@/api/biz/sysSitydutyRuleApi'
	import userApi from '@/api/sys/userApi'
	import tool from '@/utils/tool'
	let searchFormState = reactive({
		userStatus: 'ENABLE',
		jobStatus: '0'
	})
	const searchFormRef = ref()
	const table = ref()
	const formRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const columns = [
		{
			title: '姓名/部门/职位',
			dataIndex: 'name'
		},
		{
			title: '工号',
			dataIndex: 'jobNum'
		},
		{
			title: '状态',
			dataIndex: 'statusName'
		},
		{
			title: '类型',
			dataIndex: 'userTypeName'
		},
		{
			title: '上下班时间',
			dataIndex: 'workTime'
		}
	]
	const selectedRowKeys = ref([])
	const statusData = tool.dictList('COMMON_STATUS')
	const jobStatusData = tool.dictList('USER_JOB_STATUS')
	const treeDefaultExpandedKeys = ref([])
	const treeData = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	nextTick(() => {
		// 机构选择器数据
		userApi.userOrgTreeSelector().then((res) => {
			if (res !== null) {
				treeData.value = res
				// 默认展开2级
				treeData.value.forEach((item) => {
					// 因为0的顶级
					if (item.parentId === '0') {
						treeDefaultExpandedKeys.value.push(item.id)
						// 取到下级ID
						if (item.children) {
							item.children.forEach((items) => {
								treeDefaultExpandedKeys.value.push(items.id)
							})
						}
					}
				})
			}
		})
	})
	const loadData = (parameter) => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		return sysSitydutyRuleApi.userRoster(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		table.value.refresh(true)
	}
	// 删除
	const deleteSysSitydutyRule = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		sysSitydutyRuleApi.sysSitydutyRuleDelete(params).then(() => {
			table.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchSysSitydutyRule = (params) => {
		sysSitydutyRuleApi.sysSitydutyRuleDelete(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}
</script>

<style lang="less" scoped>
	.userInfo {
		display: flex;
		width: 300px;
		.userMsg p {
			margin-bottom: 3px;
		}
	}
</style>
